/**
 * New node file
 */
define([
	"jquery",
	"knockout",
	"baseview",
	"text!static/pages/super/tpl/managelist.tpl.html",
	"common"
], function(
	$,
	ko,
	BaseView,
	template,
	Common
) {
	var self;
	var view = BaseView.extend({
		setTemplate: function() {
			this.el.html("").html(template);
			Common.breadcrumb();
			var gridoptions = [{
					"field": "managercode",
					"dataType": "String",
					"title": "管理员编码",
					"width": "25%"
				},
				{
					"field": "managername",
					"dataType": "String",
					"title": "管理员名称",
					"width": "25%"
				},
				{
					"field": "isenable",
					"dataType": "String",
					"title": "是否启用",
					"width": "10%",
					"renderType": "renderIsEnable"
				},
				{
					"field": "createtime",
					"dataType": "String",
					"title": "创建时间",
					"width": "25%"
				},
				{
					"field": "id",
					"dataType": "String",
					"title": "操作",
					"renderType": "renderOptions",
					"width": "15%"
				}

			];
			var gridHtml = Common.generateGridOption(gridoptions);
			$("#managergrid").html(gridHtml)
		},
		initHistoryParams: function() {
			var historyValue = Common.session.getHistoryValue();
			var hv_pageindex = historyValue['hv_pageindex'];
			var hv_pagesize = historyValue['hv_pagesize'];
			var userInputval = historyValue['userInputval'];
			if(hv_pageindex) {
				this.viewmodel.pageIndex(hv_pageindex)
			}
			if(hv_pagesize) {
				this.viewmodel.pageSize(hv_pagesize)
			}
			if(userInputval) {
				this.viewmodel.userInputval(userInputval)
			}
		},
		fireEvents: function() {
			self = this;
			self.viewmodel.queryManagerList();
		},
		events: {
			queryManagerList: function() {
				var pageIndex = self.viewmodel.pageIndex();
				var pageSize = self.viewmodel.pageSize();
				self.viewmodel.managerDataTable.pageIndex(pageIndex)
				self.viewmodel.managerDataTable.pageSize(pageSize)
				var managercode = self.viewmodel.userInputval()
				//ajax请求，
				Common.wrapajax({
					url: Common.baseUrl + "/manager/getAll",
					contentType: "application/x-www-form-urlencoded",
					type: "post",
					data: {
						ucode: Common.userContext().ucode
					},
					success: function(data) {
						var mgers = [];
						if(data && data.length > 0) {
							for(var i = 0; i < data.length; i++) {
								var it = {
									data: data[i],
									rowId: i + 1
								};
								mgers.push(it);
							}
						}
						self.viewmodel.managerDataTable.trigger($.DataTable.ON_LOAD, mgers);
					}
				});
			},
			del: function(id) {
				if("" == id || null == id) {
					Common.toastr().error("没有获取到管理员ID");
					return;
				}
				Common.wrapajax({
					url: Common.baseUrl + "/manager/deletebyid",
					contentType: "application/x-www-form-urlencoded",
					type: "post",
					data: {
						ucode: Common.userContext().ucode,
						id: id
					},
					success: function(data) {
						if(data.code == "success") {
							Common.toastr().success("删除系统管理员成功！");
						} else {
							Common.toastr().error(data.errormsg);
						}
						window.location.href = "#/pages/super/manager";
					}

				});

			},
			/*页面切换*/
			pageChange: function (pageIndex) {
				self.viewmodel.pageIndex(pageIndex)
				self.viewmodel.queryManagerList()
			},
			sizeChange:function(pageSize){
			  self.viewmodel.pageIndex(0)
			  self.viewmodel.pageSize(pageSize)
			  self.viewmodel.queryManagerList()
			},
		},
		viewmodel: {
			el: "#manager",
			// 历史记录
			userInputval: ko.observable(''),
			pageIndex: ko.observable(0),
			pageSize: ko.observable('10'),

			managerinfo: new $.DataTable({
				params: {},
				meta: {
					'id': {},
					'managecode': {},
					'managename': {},
					'idenable': {},
					'createtime': {}
				},
				pageSize: 10
			}),
			managerDataTable: new $.DataTable({
				params: {},
				meta: {
					'id': {},
					'managecode': {},
					'managename': {},
					'isenable': {},
					'createtime': {}
				},
				pageSize: 10
			}),
			renderOptions: function(params) {
				//预先定义好操作按钮的内容，common组件总自动渲染
				var operateList = [{
						type: "click",
						iconCls: "fa-edit",
						title: '修改',
						style: " j-edit",
						placement: "left",
						"data-bind": params.value
					},
					{
						type: "click",
						iconCls: "fa-trash",
						title: '删除',
						style: " j-refperson m-r-sm j-delete",
						placement: "left",
						"data-bind": params.value
					}
				];
				Common.renderColumn.renderOperate(params, operateList);
			},
			showpage: function(data) {
				if(!data || data.length == 0) {
					$(".pagination").css("display", "none");
				} else {
					$(".pagination").css("display", "inline-block");
				}
			},
			renderIsEnable: function(params) {
				var displayValue = "";
				if(params.value == 1) {
					displayValue = "<font color='green'>已启用</font>"
				} else {
					displayValue = "<font color='red'>已停用</font>"
				}
				params.element.innerHTML = displayValue;
			},
			add: function() {
				Common.store.set("temp_manageredit_id", null)
				window.location.hash = "#/pages/super/manageredit"
			}
		},
		render: function() {

		},
		bindEvents: function() {
			$(".ibox-content").delegate(".j-edit", "click", function(data) {
				Common.store.set("temp_manageredit_id", $(this).attr("data-bind"))
				window.location.hash = "/pages/super/manageredit"
			});
			window.router.addRouter("#/pages/super/manageredit");
			$(".ibox-content").delegate(".j-delete", "click", function() {
				var id = $(this).attr("data-bind");
				var dialog = Common.alertDialog({
					title: "提示",
					content: "确认要删除系统管理员？",
					ok: function() {
						self.events.del(id)
					},
					cancel: function() {
						//window.location.href="#/pages/super/managerdelete";
					}
				});
				dialog.showModal();
			});
		}

	});
	return view;
})